/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package yassp.client;

import java.net.*;
import java.io.*;
import java.security.NoSuchAlgorithmException;
import java.util.Queue;
import java.util.logging.Level;
import java.util.logging.Logger;
import yassp.*;

/**
 *
 * @author pierre
 */
public class Connection extends Thread {

    String address = "127.0.0.1";
    int port = 12345;
    private Socket server;
    private int blockSize = 8; // blocksize temporary hardcoded
    // files streams
    private BufferedInputStream bis;
    private BufferedOutputStream bos;
    private InputStream is = null;
    private BufferedReader in = null;
    private PrintWriter out = null;
    private byte[] buf = new byte[blockSize];
    String login;
    String passwd;
    String root;
    int state;
    private boolean stop;
    // Queue de commande à exécuter
    Queue<Command> commandQueue;

    public Connection() throws IOException {
        try {
            System.out.println("Connecting at " + address + ":" + port + "...");
            server = new Socket(address, port);
            System.out.println("Connection success");
        } catch (UnknownHostException e) {
            System.out.println("Unknown host " + address + ":" + port);
            System.err.println(e);
            System.exit(1);
        }
//        try {
//            /* obtain an input stream to this client ... */
//            in = new BufferedReader(new InputStreamReader(
//                        server.getInputStream()));
//            /* ... and an output stream to the same client */
//            out = new PrintWriter(server.getOutputStream(), true);
//        } catch (IOException e) {
//            System.err.println(e);
//            return;
//        }
//        is = server.getInputStream();


        try {
            bis = new BufferedInputStream(server.getInputStream());
            bos = new BufferedOutputStream(server.getOutputStream());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

    @Override
    public void run() {
        if(!Login())
            return;
        Command c;
        PipedInputStream pis;
        stop = false;
        while (!stop) {
            c = commandQueue.remove();
            if (c == null) { //Pas de commande à executer, on s'endort pour 0.2s et on recommence
                try {
                    Thread.sleep(200);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
                }
                continue;
            }
            try {
                //acquisition du pipe contenant la commande
                pis = c.getCommand();
                //envoi de la commande au serveur
                while ((pis.read(buf)) != -1) {
                    bos.write(buf);
                }
            } catch (IOException ex) {
                Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
                continue;
            }
            c.recvResponse(bis);
        }

    }

    public void pushCommand(Command c) {
        commandQueue.add(c);
    }

    public boolean isCommandQueueEmpty() {
        return (commandQueue.size() == 0);
    }

    private boolean Login() {
        //TODO: Ecrire le code pour la gestion du lgin
        return true;
    }
}
